package com.votesys.service;

import java.sql.Blob;
import java.sql.Timestamp;
import java.util.List;

import com.votesys.model.Votetopic;
import com.votesys.model.VotetopicDAO;


public class TopicServiceImpl implements ITopicService{
	private VotetopicDAO VotetopicDAO;

	public VotetopicDAO getVotetopicDAO() {
		return VotetopicDAO;
	}

	public void setVotetopicDAO(VotetopicDAO votetopicDAO) {
		VotetopicDAO = votetopicDAO;
	}

	public Votetopic getVoteTopic(int topicID){
		return VotetopicDAO.findById(topicID);
		
	}
	public List getTopicList(){
		return VotetopicDAO.findAll();		
	}
	
	public void addNewTopic(String topicName,String topicDescription,String logoName,
			byte[] logo,Integer showPattern,Integer checkOut,Integer minCount,Integer maxCount,Integer flag
	){
		Votetopic votetopic = new Votetopic();
		votetopic.setTopicName(topicName);
		votetopic.setTopicDescription(topicDescription);
		votetopic.setLogoName(logoName);
		votetopic.setLogo(logo);
		votetopic.setShowPattern(showPattern);
		votetopic.setCheckOut(checkOut);
		votetopic.setMinCount(minCount);
		votetopic.setMaxCount(maxCount);
		votetopic.setFlag(flag);
		System.out.println(topicName);
		VotetopicDAO.save(votetopic);
	}
	
	public void updateTopic(Votetopic votetopic){
		if(votetopic!=null){
		VotetopicDAO.merge(votetopic);
		}
	}

	@Override
	public void deleteTopic(Votetopic votetopic) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Votetopic getRuntimeTopic() {
		// TODO Auto-generated method stub
		Votetopic tempVoteTopic = null;
		List templist = VotetopicDAO.findByFlag(1);
		if(!templist.isEmpty() && templist.size()==1){
			tempVoteTopic = (Votetopic) templist.get(0);
		}else{
			System.out.println("get runtime topic error");
		}
		return tempVoteTopic;
	}
	
	

}
